#coding=utf-8
#测试平台注册
#创建者：陈强
#创建日期：2019/03/29

from TestWeb import mysql_connect
import time, datetime
#写入数据
def write_sql(mobile,password,name):
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))  # 数据产生日期
    connect = mysql_connect.mysql_connect()[0]  # connect是由connect_mysql函数返回的第一个值
    cursor = connect.cursor()
    # SQL 语句
    sql = "insert into mysql.TestWeb_user(mobile,nick_name,password,created_at) values('%s','%s','%s','%s')" % (str(mobile),str(name),str(password),str(now))
    # 执行sql语句
    input = cursor.execute(sql)
    connect.commit()  # 实例提交命令
    cursor.close()
    connect.close()

#查询账号是否注册
def check_sql(keys,values):
    connect = mysql_connect.mysql_connect()[0]  # connect是由connect_mysql函数返回的第一个值
    cursor = connect.cursor()
    # SQL 语句
    sql = "SELECT " + keys + " from mysql.TestWeb_user WHERE mobile ="+str(values)
    # 执行sql语句
    cursor.execute(sql)
    check = cursor.fetchall()
    try:
        connect.commit()  # 实例提交命令
        check = str(check[0][0])
        return check
    except:
        return '无此数据'

#注册
def register(mobile,password,name):
    #首先判断是否注册过
    if mobile == check_sql('mobile',mobile):
        return '该手机号码已注册'
    else:

        write_sql(mobile,password,name)
        return '注册成功'


if __name__ == "__main__":
    register('13800010001','cq123321','李大炮')